﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;

namespace IntelligentCMS
{
    public class CMSFile
    {
        /// <summary>
        /// Gets a file that is currently stored in the DB.
        /// Give it an id to get the file.
        /// </summary>
        /// <param name="id"></param>
        /// <returns>File</returns>
        public static string GetFile(long fileid)
        {

            string filename = string.Empty;
            CMSDbDataLayer file = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            file.AddQuery("SELECT [Filename] FROM File WHERE ID = " + fileid);
            file.SelectQuery = true;
            file.Execute();

            filename = file.Data.Rows[0]["Filename"].ToString();
            return filename;
            
        }

        /// <summary>
        /// For when we need to delete a file from the server.
        /// </summary>
        /// <param name="fileid"></param>
        public static void DeleteFile(long fileid)
        {
            string file = string.Empty;
            CMSDbDataLayer filename = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            filename.AddQuery("SELECT [Filename] FROM File WHERE ID = " + fileid);
            filename.SelectQuery = true;
            filename.Execute();

            file = filename.Data.Rows[0]["Filename"].ToString();

            File.Delete(file);





        }

        public static void UpdateFile() {


        }


        /// <summary>
        /// Saves a file to the db, and returns it's new id. Set the copyrandom to true for Content, and false for plugins.
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static long SaveAndGetFileID(string filename, bool copyrandom)
        {
            long fileid = -1;
            
            if (copyrandom)
            {
                FileInfo newInfo = new FileInfo(filename);
                string originalfilename = filename;
                filename = Path.GetRandomFileName() + newInfo.Extension;
                while (File.Exists(filename))
                {
                    filename = Path.GetRandomFileName() + newInfo.Extension;

                }

                File.Move(originalfilename, "tmp/" + filename);
            }

            CMSDbDataLayer file = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            file.AddQuery("SELECT [ID] FROM File WHERE File = " + filename);
            file.SelectQuery = true;
            file.Execute();

            fileid = long.Parse(file.Data.Rows[0]["ID"].ToString());



            return fileid;
            

           
        }

        
    }
}
